package com.eibus.applicationconnector.ftp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Properties;
import java.io.FileInputStream;

import com.eibus.xml.nom.Document;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class FTP_Prerequisites extends TestCase{

	private static final int PORT = 21;
	BufferedReader br ;
	 PrintWriter pw ;

	public FTP_Prerequisites() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */


	protected void setUp() throws Exception
	{
		super.setUp();

	}

	protected void tearDown() throws Exception
	{
		super.tearDown();
	}

	public void testftpConnect()throws Exception
	    {
			Properties prop = new Properties();
			prop.load(new FileInputStream(System.getenv("BCPTESTS_HOME")+"/tests/soapunit/ftpconn_soapunit.properties"));
	        String sServerName = prop.getProperty("ftp.server.name");
			Socket csock = new Socket(sServerName, PORT);//connects to the server socket
	        InputStream cis = csock.getInputStream();
	        br = new BufferedReader(new InputStreamReader(cis));
	        OutputStream cos = csock.getOutputStream();
	        pw = new PrintWriter(cos, true);
	        String message = "";
	        Integer msgCode;
	        boolean nfeOccured = false;
	        do
	        {
	            message = br.readLine();
	            while(message==null||message.equals(""))
	            {
	                message = br.readLine();
	            }
	            nfeOccured = false;
	            try
	            {
	                msgCode = new Integer(message.substring(0,3));
	            }
	            catch(Exception nfe)
	            {
	            	nfeOccured = true;
	            	continue;
	            }
	        }while ( nfeOccured || !message.substring(3,4).equals(" ") );
	        if(message.substring(0,3).equals("421")||message.substring(0,3).equals("120"))
			{
	        	System.out.println("failed "+message);
	 		}
	        System.out.println("sucess "+message);
	        this.assertTrue(true);
	    }

	   String sendCommand(String cmd) throws Exception
	    {

	        String message = "";
	        StringBuffer strbuff = new StringBuffer();

	        try
	        {
	            pw.println(cmd);
	            Integer msgCode;
	            boolean nfeOccured = false;
	            do
	            {
	                message = br.readLine();
	                while(message==null||message.equals(""))
	                {
	                    message = br.readLine();
	                }
	                nfeOccured = false;
	                try
	                {
	                    msgCode = new Integer(message.substring(0,3));
	                }
	                catch(Exception nfe)
	                {
	                	nfeOccured = true;
	                	continue;
	                }
	            }
	            while ( nfeOccured || !message.substring(3,4).equals(" ") );
	        }
	        catch(Exception e)
	        {
	        	e.printStackTrace();
	            message = e.toString();
	        }
	        return(message);
	    }


	   public  static Test suite()
		{

			TestSuite ts = new TestSuite();
			ts.addTestSuite(FTP_Prerequisites.class);
			System.out.println("automated called");
			return ts;
		}
	  public static void main(String[] args)
	  {
		  /*
		  FTP_Prerequisites ftpTester = new FTP_Prerequisites();
		  try {
			ftpTester.testftpConnect();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/

		junit.textui.TestRunner.run(FTP_Prerequisites.suite());
	  }


}
